package com.rew.canteenchefs.managers.imp;

import com.rew.canteenchefs.daos.IEUserDao;
import com.rew.canteenchefs.managers.IEUserManager;
import com.rew.canteenchefs.models.EUser;
import com.rew.canteenchefs.models.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import static com.rew.canteenchefs.enumerate.ResultTypeEnum.*;


/**
 * User - Manager 实现层.
 *
 * Created by HuiWen Ren on 2016/12/2.
 */
@Component
public class EUserManager implements IEUserManager {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(EUserManager.class);

    @Autowired
    IEUserDao userDao;

    /**
     * 插入用户.
     *
     * @param eUser user实体
     * @return 插入结果
     */
    @Override
    public Result<Integer> insertUser(EUser eUser) {
        Result<Integer> result = new Result<>();
        try {
            result.setBean(userDao.insertUser(eUser));
            if(result.getBean().equals(SUCCESS.getValue())){
                result.setCode(SUCCESS.getValue());
                result.setMessage(SUCCESS.getDescription());
            }

        } catch(DuplicateKeyException e) {
            LOGGER.error("DuplicateKeyFAILED", e);
            result.setCode(DUPLICATE.getValue());
            result.setMessage(DUPLICATE.getDescription());

        } catch(RuntimeException e){
            LOGGER.error("insertFAILED", e);
            result.setCode(FAILED.getValue());
            result.setMessage(FAILED.getDescription());
        }
        return result;
    }

    /**
     * 核对密码（非初始）.
     *
     * @param card_id 校园卡账号
     * @param password 请求密码
     * @return 核对结果
     */
    @Override
    public Result<EUser> checkPassword(String card_id, String password) {
        Result<EUser> result = new Result<>();
        try {
            result.setBean(userDao.selectUser(card_id));
            Assert.isTrue(
                    password.equals(result.getBean().getPassword())
            );
            result.setCode(SUCCESS.getValue());
            result.setMessage(SUCCESS.getDescription());
        } catch (RuntimeException e){
            result.setCode(PSD_WRONG.getValue());
            LOGGER.error("LoadFAILED", e);
            result.setMessage(PSD_WRONG.getDescription());
        }
        return result;
    }

    /**
     * 查询用户.
     *
     * @param card_id 待查询校园卡号
     * @return 查询结果，无则为空
     */
    @Override
    public Result<EUser> selectUser(String card_id) {
        Result<EUser> result = new Result<>();
        try {
            result.setBean(userDao.selectUser(card_id));
            if(result.getBean() != null){
                result.setCode(SUCCESS.getValue());
                result.setMessage(SUCCESS.getDescription());
            } else {
                result.setCode(NOT_EXIST.getValue());
                result.setMessage(NOT_EXIST.getDescription());
            }
        } catch (RuntimeException e){
            LOGGER.error("USER_SelectFAILED:", e);
            result.setCode(FAILED.getValue());
            result.setMessage(FAILED.getDescription());
        }
        return result;
    }

    /**
     * 更新用户.
     *
     * @param eUser 更新用户
     * @return 更新结果
     */
    @Override
    public Result<Integer> updateUser(EUser eUser) {
        Result<Integer> result = new Result<>();
        try {
            result.setCode(userDao.updateUser(eUser));
            result.setMessage(SUCCESS.getDescription());
        } catch (RuntimeException e){
            LOGGER.error("UESR_UpdateFAILED:", e);
            result.setMessage(FAILED.getDescription());
            result.setCode(FAILED.getValue());
        }
        return result;
    }

    @Override
    public Result<Integer> deleteUser(String card_id) {
        Result<Integer> result = new Result<>();
        try {
            result.setBean(userDao.deleteUser(card_id));
            result.setCode(SUCCESS.getValue());
            result.setMessage(SUCCESS.getDescription());
        } catch (RuntimeException e){
            result.setCode(FAILED.getValue());
            LOGGER.error("USER_DeleteFAILED", e);
            result.setMessage(FAILED.getDescription());
        }
        return result;
    }
}
